package com.ebupt.migu.music.oscloud.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.oscloud.mapper.OSCloudMapper;
import com.ebupt.migu.music.oscloud.service.OSCloudService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author liuyangyang@e-byte.com
 * @description: TODO
 * @date 2023/7/10 17:08
 */
@Slf4j
@Service
public class OSCloudServiceImpl implements OSCloudService {
    @Autowired
    private OSCloudMapper mapper;

    /**
     * 查询运营师图标明细
     *
     * @return
     */
    @Override
    public ResultObject detail() {
        // 客群类型分布情况
        List<HashMap<String, String>> customers = mapper.queryCustomers();
        // 离线活动分布情况
        List<HashMap<String, String>> offlineActivities = mapper.queryOfflineActivities();
        // 离线活动总数
        int totalOfflineActivity = offlineActivities.size();
        // 未开始活动总数
        int prepareOfflineActivity = 0;
        // 暂停中活动总数
        int pausedOfflineActivity = 0;
        // 已结束活动总数
        int finishedOfflineActivity = 0;
        // 进行中活动总数
        int processOfflineActivity = 0;
        // 其他状态活动总数
        int otherOfflineActivity = 0;
        // 被活动应用的客群集合【排除被驳回的活动】
        Set<String> groupIds = new HashSet<>();
        for (HashMap<String, String> activity : offlineActivities) {
            groupIds.add(activity.get("groupId"));
            // 未开始活动计数
            if ("3".equals(String.valueOf(activity.get("executeStatus")))) {
                prepareOfflineActivity++;
                continue;
            }
            // 暂停中活动计数
            if ("4".equals(String.valueOf(activity.get("executeStatus"))) && "1".equals(String.valueOf(activity.get("pause")))) {
                pausedOfflineActivity++;
                continue;
            }
            // 进行中活动计数
            if ("4".equals(String.valueOf(activity.get("executeStatus")))) {
                processOfflineActivity++;
                continue;
            }
            // 已中止活动计数
            if ("6".equals(String.valueOf(activity.get("executeStatus")))) {
                finishedOfflineActivity++;
                continue;
            }
            // 其他状态中的活动总数
            otherOfflineActivity++;
        }
        JSONObject offlineActivityDetail = new JSONObject();
        offlineActivityDetail.put("totalOfflineActivity", totalOfflineActivity);
        offlineActivityDetail.put("prepareOfflineActivity", prepareOfflineActivity);
        offlineActivityDetail.put("pausedOfflineActivity", pausedOfflineActivity);
        offlineActivityDetail.put("finishedOfflineActivity", finishedOfflineActivity);
        offlineActivityDetail.put("processOfflineActivity", processOfflineActivity);
        offlineActivityDetail.put("otherOfflineActivity", otherOfflineActivity);
        // 客群总量
        int totalCustomer = customers.size();
        // 被应用的客群总量
        int totalCustomerApply = groupIds.size();
        // 文件上传客群总量
        int importCustomer = 0;
        // 被应用的文件上传客群总量
        int importCustomerApply = 0;
        // FTP客群总量
        int ftpCustomer = 0;
        // 被应用的FTP客群总量
        int ftpCustomerApply = 0;
        // “重度用户”客群总量
        int severeCustomer = 0;
        // 被应用的“重度用户”客群总量
        int severeCustomerApply = 0;
        // 重组客群总量
        int restructureCustomer = 0;
        // 被应用的重组客群总量
        int restructureCustomerApply = 0;
        // 标签客群总量
        int labelCustomer = 0;
        // 被应用的标签客群总量
        int labelCustomerApply = 0;
        for (HashMap<String, String> customer : customers) {
            // FTP客群计数
            if ("3".equals(String.valueOf(customer.get("createWay"))) && "1".equals(String.valueOf(customer.get("isFtp")))) {
                ftpCustomer++;
                if (groupIds.contains(customer.get("groupId"))) {
                    ftpCustomerApply++;
                }
                continue;
            }
            // 导入客群计数
            if ("3".equals(String.valueOf(customer.get("createWay")))) {
                importCustomer++;
                if (groupIds.contains(customer.get("groupId"))) {
                    importCustomerApply++;
                }
                continue;
            }
            // “重度用户”客群计数
            if ("12".equals(String.valueOf(customer.get("createWay")))) {
                severeCustomer++;
                if (groupIds.contains(customer.get("groupId"))) {
                    severeCustomerApply++;
                }
                continue;
            }
            // 重组客群计数
            if ("4".equals(String.valueOf(customer.get("createWay"))) || "5".equals(String.valueOf(customer.get("createWay"))) || "6".equals(String.valueOf(customer.get("createWay"))) || "7".equals(String.valueOf(customer.get("createWay"))) || "8".equals(String.valueOf(customer.get("createWay")))) {
                restructureCustomer++;
                if (groupIds.contains(customer.get("groupId"))) {
                    restructureCustomerApply++;
                }
                continue;
            }
            labelCustomer++;
            if (groupIds.contains(customer.get("groupId"))) {
                labelCustomerApply++;
            }
        }
        JSONObject customerStructureDetail = new JSONObject();
        customerStructureDetail.put("totalCustomer", totalCustomer);
        customerStructureDetail.put("totalCustomerApply", totalCustomerApply);
        customerStructureDetail.put("importCustomer", importCustomer);
        customerStructureDetail.put("importCustomerApply", importCustomerApply);
        customerStructureDetail.put("ftpCustomer", ftpCustomer);
        customerStructureDetail.put("ftpCustomerApply", ftpCustomerApply);
        customerStructureDetail.put("severeCustomer", severeCustomer);
        customerStructureDetail.put("severeCustomerApply", severeCustomerApply);
        customerStructureDetail.put("restructureCustomer", restructureCustomer);
        customerStructureDetail.put("restructureCustomerApply", restructureCustomerApply);
        customerStructureDetail.put("labelCustomer", labelCustomer);
        customerStructureDetail.put("labelCustomerApply", labelCustomerApply);

        List<HashMap<String, String>> onlineActivities = mapper.queryOnlineActivities();
        // 变色龙活动总数
        int totalOnlineActivity = onlineActivities.size();
        // 未开始活动总数
        int prepareOnlineActivity = 0;
        // 暂停中活动总数
        int pausedOnlineActivity = 0;
        // 已结束活动总数
        int finishedOnlineActivity = 0;
        // 进行中活动总数
        int processOnlineActivity = 0;
        // 其他状态活动总数
        int otherOnlineActivity = 0;
        for (HashMap<String, String> activity : onlineActivities) {
            if ("3".equals(String.valueOf(activity.get("activityStatus")))) {
                prepareOnlineActivity++;
                continue;
            }
            if ("5".equals(String.valueOf(activity.get("activityStatus")))) {
                pausedOnlineActivity++;
                continue;
            }
            if ("6".equals(String.valueOf(activity.get("activityStatus")))) {
                finishedOnlineActivity++;
                continue;
            }
            if ("4".equals(String.valueOf(activity.get("activityStatus")))) {
                processOnlineActivity++;
                continue;
            }
            otherOnlineActivity++;
        }
        JSONObject onlineActivityDetail = new JSONObject();
        onlineActivityDetail.put("totalOnlineActivity", totalOnlineActivity);
        onlineActivityDetail.put("prepareOnlineActivity", prepareOnlineActivity);
        onlineActivityDetail.put("pausedOnlineActivity", pausedOnlineActivity);
        onlineActivityDetail.put("finishedOnlineActivity", finishedOnlineActivity);
        onlineActivityDetail.put("processOnlineActivity", processOnlineActivity);
        onlineActivityDetail.put("otherOnlineActivity", otherOnlineActivity);

        List<HashMap<String, String>> autoTriActivities = mapper.queryAutoTriActivities();
        // 变色龙活动总数
        int totalAutoTriActivity = autoTriActivities.size();
        // 未开始活动总数
        int prepareAutoTriActivity = 0;
        // 暂停中活动总数
        int pausedAutoTriActivity = 0;
        // 已结束活动总数
        int finishedAutoTriActivity = 0;
        // 进行中活动总数
        int processAutoTriActivity = 0;
        // 其他状态活动总数
        int otherAutoTriActivity = 0;
        for (HashMap<String, String> activity : autoTriActivities) {
            if ("3".equals(String.valueOf(activity.get("tacStatus")))) {
                prepareAutoTriActivity++;
                continue;
            }
            if ("4".equals(String.valueOf(activity.get("tacStatus"))) && "0".equals(String.valueOf(activity.get("pause")))) {
                pausedAutoTriActivity++;
                continue;
            }
            if ("6".equals(String.valueOf(activity.get("tacStatus")))) {
                finishedAutoTriActivity++;
                continue;
            }
            if ("4".equals(String.valueOf(activity.get("tacStatus")))) {
                processAutoTriActivity++;
                continue;
            }
            otherAutoTriActivity++;
        }
        JSONObject autoTriActivityDetail = new JSONObject();
        autoTriActivityDetail.put("totalAutoTriActivity", totalAutoTriActivity);
        autoTriActivityDetail.put("prepareAutoTriActivity", prepareAutoTriActivity);
        autoTriActivityDetail.put("pausedAutoTriActivity", pausedAutoTriActivity);
        autoTriActivityDetail.put("finishedAutoTriActivity", finishedAutoTriActivity);
        autoTriActivityDetail.put("processAutoTriActivity", processAutoTriActivity);
        autoTriActivityDetail.put("otherAutoTriActivity", otherAutoTriActivity);

        JSONObject detail = new JSONObject();
        detail.put("customerStructureDetail", customerStructureDetail);
        detail.put("offlineActivityDetail", offlineActivityDetail);
        detail.put("onlineActivityDetail", onlineActivityDetail);
        detail.put("autoTriActivityDetail", autoTriActivityDetail);

        ResultObject res = new ResultObject();
        res.setCode(StatusEnum.SUCCESS.getCode());
        res.setMsg(StatusEnum.SUCCESS.getMsg());
        res.setData(detail);
        return res;
    }
}
